using UnityEngine;
using System.Collections;


[AddComponentMenu ( "Chromatic2D/C2DCamera" )]
public class C2DCamera : MonoBehaviour 
{	
	public int m_uNativeResolutionX = 1280;
	public int m_uNativeResolutionY = 720;
	public bool m_bPixelPerfect = false;
	
	Transform m_content;
	
	void Awake()
	{	
		Calculate( new Vector2( Screen.width, Screen.height ) );
	}
	
	/// <summary>
	/// Calculate the specified viewSize.
	/// </summary>
	public void Calculate( Vector2 viewSize )
	{
		camera.isOrthoGraphic = true;
		camera.nearClipPlane = 0;
		
		m_content = transform.Find( "Content" );
		
		if( m_content == null ) 
		{
			m_content = (Transform)new GameObject( "Content" ).GetComponent<Transform>();
		}
		
		m_content.parent = transform;
		
		float orthographicSize = m_uNativeResolutionY / 2f;
		
		if( m_bPixelPerfect )
		{	
			orthographicSize = viewSize.y / 2f;
		}
		
		camera.orthographicSize = orthographicSize;
		
		Vector3 originOffset = new Vector3( orthographicSize * camera.aspect, -orthographicSize, -1.0f );
		
		m_content.position = transform.position - originOffset;
	}
}